<?php
namespace app\redbag\model;

use app\BaseModel;
use app\redbag\model\Record;
use think\facade\Db;

class IndexActive extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_redbag_active';


    protected $append = [

        //'status_text'
    ];

    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }



    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-03-21 15:00
     */
    public function getStatusTextAttr($value,$data){

//        if(!empty($data['start_time'])&&$data['end_time']){
//            if($data['start_time']>time()){
//
//                return '未开始';
//
//            }elseif ($data['start_time']<time()&&$data['end_time']>time()){
//
//                return '进行中';
//
//            }else{
//
//                return '已结束';
//            }
//        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:转换图片格式
     * @author chenniang
     * @DataTime: 2020-03-09 18:56
     */
    public function getImgsAttr($value,$data){

        $img = transImagesOne([$value],['imgs'],$data['uniacid']);

        return !empty($img[0])?$img[0]:'';


    }


    /**
     * @param $value
     * @功能说明:封面图
     * @author chenniang
     * @DataTime: 2020-03-09 19:02
     */
    public function getCoverAttr($value,$data){

        $img = transImagesOne([$value],['cover'],$data['uniacid']);

        return !empty($img[0])?$img[0]:'';
    }

    /**
     * @param $dis
     * @param $page
     * @功能说明:活动列表
     * @author chenniang
     * @DataTime: 2020-03-09 18:42
     */

    public function activeList($dis,$com_dis,$staff_dis,$staff_id){

        $data = $this->alias('a')
                ->join('longbing_card_redbag_company b','a.id = b.active_id','left')
                ->where($dis)
                ->whereOr(function ($query) use ($com_dis){
                     $query->where($com_dis);
                })->whereOr(function ($query) use ($staff_dis){
                     $query->where($staff_dis);
                })
                ->field('a.*')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate(10)
                ->toArray();

        if(!empty($data['data'])){
            //记录模型
            $record_model  = new Record();
            //员工发放模型
            $staff_record_model = new staffRecord();

            foreach ($data['data'] as &$v){
                //使用数量
                $record_dis = [
                    //状态
                    'status'    => 1,
                    //员工
                    'staff_id'  => $staff_id,
                    //活动
                    'active_id' => $v['id']
                ];
                //使用数量
                $v['use_count'] = $record_model->where($record_dis)->count();

                $record_dis['status'] = 0;
                //是否展示到首页
                $is_show = $staff_record_model->recordInfo($record_dis);

                $v['is_show'] = !empty($is_show)&&$is_show['is_show']==1?1:0;
                //如果已经领取完了就改变状态
//                if($v['use_count']>=$v['num']){
//
//                    $staff_record_model->where($record_dis)->update(['status'=>1]);
//
//                }
                unset($record_dis['status']);
                //这个红包是否发放
                $staff_record = $staff_record_model->recordInfo($record_dis);
                //红包状态
                if(!empty($staff_record)){

                    $v['redbag_status'] = $staff_record['status'];

                }else{
                    //-2就是还没有发放
                    $v['redbag_status'] = -2;
                }
                //转换时间
                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-10 10:30
     * @功能说明:活动详情
     */
    public function activeInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:添加活动
     */
    public function activeAdd($data)
    {
        //开始时间
        $data['create_time'] = time();
        //更新时间
        $data['update_time'] = time();
        //状态
        $data['status'] = -1;

        $this->insert($data);

        $id = $this->getLastInsID();

        return $id;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-10 15:22
     * @功能说明:支付回调
     */
    public function payResult($order_code,$transaction_id){

        $data = $this->activeInfo(['order_code'=>$order_code]);

        if(!empty($data)&&$data['status']==-1){

            $this->where(['order_code'=>$order_code])->update(['status'=>1,'transaction_id'=>$transaction_id]);

        }
        return true;
    }










}